const express = require('express');
const app = express();
// 让express识别从客户端提交过来的json
app.use(express.json())

//允许跨域
app.use(require('cors')())

const connection = require('./db.js')

// 显示角色列表
app.get('/api/roleList', async (rep, res) => {
    let sql = "select rid,rname from roles";
    connection.query(sql, (err, result) => {
        if (err) {
            res.send({
                meta: {
                    msg: err,
                    status: 400
                }
            })
        } else {
            res.send({
                result,
                meta: {
                    msg: '获取角色列表',
                    status: 200
                }
            })
        }
    })
})

//删除角色
app.delete('/api/roleList/:id', async (rep, res) => {
    let sql = 'delete from roles where rid=?';
    let params = rep.params.id;
    connection.query(sql, params, (err) => {
        if (err) {
            console.log(err)
            console.log("删除角色失败!");
            return
        }
        else {
            res.send({
                status: true
            });
        }
    })
})

//添加角色
app.post('/api/addRole', async (rep, res) => {
    let sql = 'insert into roles(rid,rname) values(?,?)';
    let sql2 = 'select rname from roles where rname=?';
    let { rid, rname } = rep.body;

    connection.query(sql2, rname, (err, result) => {
        if (err) {
            res.send({
                meta: {
                    msg: err,
                    status: 404
                }
            })
        }
        else {
            if (!result || result.length == 0) {
                connection.query(sql, [rid, rname], (err2) => {
                    if (err2) {
                        res.send({
                            meta: {
                                msg: '新增失败',
                                status: 400
                            }
                        })

                    }
                    else {
                        res.send({
                            meta: {
                                msg: '新增成功！',
                                status: 200
                            }
                        })
                    }
                })
            }
            else {
                res.send({
                    meta: {
                        msg: '角色名已存在',
                        status: 400
                    }
                })
            }
        }
    })
})


//查看为？的用户名单
app.get('/api/showRoles/:id',async(rep,res)=>{
    let sql="select u.username,r.rname from users u,roles r WHERE u.rid=r.rid and r.rid=?"
    let params=rep.params.id
    console.log(params)
    connection.query(sql,params,(err,result)=>{
        if(err){
            res.send({
                meta: {
                    msg: err,
                    status: 404
                }
            })
        }
        else{
                res.send({
                    result,
                    meta: {
                            msg: '查询成功！',
                            status: 200
                        }
                })
        }
    })
})

module.exports = app